<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleadd('1')"
          :disabled="!routeproductbomList.length">生成良品退料单</el-button>
        <el-button type="warning" icon="el-icon-plus" size="mini" @click="handleadd('2')"
          :disabled="!routeproductbomList.length">生成不良品退料单</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="routeproductbomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单编号" width="130" align="center" prop="fmobillNo" />
      <el-table-column label="物料编号" width="130" align="center" prop="itemCode" />
      <el-table-column label="物料名称" width="130" align="center" prop="itemName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" width="260" align="center" prop="specification" :show-overflow-tooltip="true" />

      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="总数量" align="center" prop="fqty" width="130"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { selectBomLineList, addProProductBack } from "@/api/mes/pro/workorder";
export default {
  name: "returnPiece",
  props: {
    routeId: null,
    productId: null,
    processId: null,
    optType: null,
    buttonType: null,
    workorderId: null,
    workorderCode: null
  },
  dicts: ["mes_workorder_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品制程物料BOM表格数据
      routeproductbomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        workorderCode: this.workorderCode
        // pageNum: 1,
        // pageSize: 10
      },
      multipleSelection: []//选择后的数据
    };
  },
  created() {
    this.getList();
  },
  watch: {
    workorderCode: function (val) {
      this.workorderCode = val;
      this.queryParams.workorderCode = val;
      if (val) {
        this.getList();
      }
    }
  },
  methods: {
    //多选框事件
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /** 查询产品制程物料BOM列表 */
    getList() {
      this.loading = true;
      selectBomLineList(this.workorderCode).then(response => {
        this.routeproductbomList = response.data || [];
        // this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //生成退料单
    handleadd(type) {
      const that = this
      if (that.multipleSelection.length == 0) {
        that.$modal.msgError("请先勾选需要退料的数据");
        return false
      }
      const param = {
        workOrderId: that.workorderId,
        reasonVal: type,
        // list: that.routeproductbomList
        list: that.multipleSelection
      }
      this.$modal
        .confirm("是否生成退料单")
        .then(function () {
          addProProductBack(param).then((res) => {
            console.log('res', res)
            if (res && res.code === 200) {
              that.$modal.msgSuccess("生成退料单成功");
              that.getList();
              //清空勾选项
              this.multipleSelection = []
            } else {
              that.$modal.msgError(res.msg);
            }
          })
        })
        .catch((err) => {
          console.log(err)
        });
      //   addProProductBack(this.routeproductbomList).then(res => {
      //     if (res.code == 200) {
      //       this.$message(res.msg);
      //     }
      //   });
    }
  }
};
</script>